<template>
    <div>
        <table class="table">
            <tbody class="table">
                <tr>
                    <td><span style="color: red;">*</span>机构名称</td>
                    <td>
                        <input type="text" v-model="data.oname">
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span>机构封面</td>
                    <td>
                        <input type="file" @change="up">
                        <img :src="'https://localhost:7017'+data.oimg" style="width: 100px;height: 100px;">
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span>特色服务</td>
                    <td>
                        <select v-model="data.sid">
                            <option :value="0">==请选择==</option>
                            <option v-for="ss in showdata" :value="ss.sid">{{ss.sname}}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>营业时间</td>
                    <td>
                        <input type="text" v-model="data.strarServiceTime">
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span>机构地址</td>
                    <td>
                        <input type="text" v-model="data.address">
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span>联系电话</td>
                    <td>
                        <input type="text" v-model="data.phone">
                    </td>
                </tr>
                <tr>
                    <td>发布状态</td>
                    <td>
                        <input type="radio" name="s1" :value="true" v-model="data.isPublish">发布
                        <input type="radio" name="s1" :value="false" v-model="data.isPublish">未发布
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span>机构详情</td>
                    <td>
                        <textarea v-model="data.onote"></textarea>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="2">
                        <input type="button" value="保存" @click="add">
                        <input type="button" value="返回" @click="back">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';

onMounted(() => {
  showdd();  
})

const up=(s:any)=>{
    var f= s.target.files[0];
    var fd=new FormData();
    console.log(f.type);
    fd.append("files",f);
    axios.post("https://localhost:7017/api/UpFiles/UpImgs",fd).then(res=>{
        data.value.oimg=res.data;
    })
}

const showdata=ref([{
    "sid": 0,
    "sname": ""
}])

const showdd=()=>{
    axios.get("https://localhost:7017/api/Orga/GetServices").then(res=>{
        showdata.value=res.data;
    })
}

const data=ref({
  "oid": 0,
  "oname": "",
  "oimg": "",
  "sid": 0,
  "strarServiceTime": "",
  "address": "",
  "phone": "",
  "isPublish": true,
  "onote": "",
  "isDel": false
})


const router=useRouter();

const add=()=>{
    if(data.value.oname==""){
        alert("机构名称不能为空")
        return;
    }
    if(data.value.oimg==""){
        alert("机构封面必须上传")
        return;
    }
    if(data.value.sid==0){
        alert("特色服务必须选择")
        return;
    }
    if(data.value.address==""){
        alert("机构地址不能为空")
        return;
    }
    if(data.value.phone==""){
        alert("机构电话不能为空")
        return;
    }
    if(data.value.onote==""){
        alert("机构详情不能为空")
        return;
    }
    axios.post("https://localhost:7017/api/Orga/AddOrgan",data.value).then(res=>{
        if(res.data>0){
            alert("保存成功");
            router.push({
                path:'/'
            })
        }else{
            alert("保存失败")
        }
    })
}

const back=()=>{
    router.push({
        path:'/'
    })
}
</script>

<style scoped>

</style>